//
//  TypeOfClassViewController.m
//  mUBB
//
//  Created by Cosmin Stirbu on 3/8/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "TypeOfClassViewController.h"
#import "CoursesManager.h"
#import "WSError.h"
#import "Constants.h"

@interface TypeOfClassViewController ()

@end

@implementation TypeOfClassViewController

@synthesize typeOfClassTable;

- (id)initWithNibName:(NSString *)nibNameOrNil booking:(Booking *)booking
{
    self = [super initWithNibName:nibNameOrNil bundle:nil];
    if (self) {
        // Custom initialization
        _booking = booking;
        _typesOfClasses = _booking.course.typesOfClasses;
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    if (_typesOfClasses == nil) {
        // Get types of classes for the bookings course
        [Utils showLoadingOn:self];
        [[CoursesManager instance] getTypesOfClassesForCourseCode:_booking.course.courseCode delegate:self];
    }
    
}

#pragma mark - UITableView Data Source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_typesOfClasses count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    TypeOfClass *typeOfClass = [_typesOfClasses objectAtIndex:indexPath.row];
    if (typeOfClass.typeOfClassId == _booking.typeOfClass.typeOfClassId) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = typeOfClass.name;
    return cell;
}

#pragma mark - UITableView Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    _booking.typeOfClass = [_typesOfClasses objectAtIndex:indexPath.row];
    [typeOfClassTable reloadData];
}

#pragma mark - CoursesProtocol Methods

- (void)didReceiveTypesOfClassesWithSuccess:(id)object forTicket:(Ticket *)ticket {
    [Utils hideLoadingOn:self];
    if (!_isOnScreen)
        return;
    _typesOfClasses = object;
    _booking.course.typesOfClasses = _typesOfClasses;
    [typeOfClassTable reloadData];
}

- (void)didReceiveTypesOfClassesWithFailure:(id)object forTicket:(Ticket *)ticket {
    [Utils hideLoadingOn:self];
    if (!_isOnScreen)
        return;
    WSError *error = (WSError *)object;
    if (error.errorCode == StatusCodeConnectionError) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:error.errorMessage delegate:self cancelButtonTitle:NSLocalizedString(@"ALERT_CANCEL", nil) otherButtonTitles:NSLocalizedString(@"ALERT_RETRY", nil), nil];
        [alertView show];
    } else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:error.errorMessage delegate:nil cancelButtonTitle:NSLocalizedString(@"ALERT_OK", nil) otherButtonTitles: nil];
        [alertView show];
    }
}

#pragma mark - UIAlertViewDelegate Methods

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        // Get types of classes for the bookings course
        [Utils showLoadingOn:self];
        [[CoursesManager instance] getTypesOfClassesForCourseCode:_booking.course.courseCode delegate:self];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setTypeOfClassTable:nil];
    [super viewDidUnload];
}
@end
